<template>
  <Modal v-model="show"
         class="modal-collector"
         title="租户切换"
         width=600>
    <div v-if="model">
      <ModelComponent ref="item"
                      :model="model"
                      :hasCheck="false"
                      @actionTriggerInner="listActionTrigger"></ModelComponent>
    </div>
    <div v-else>
      <Spin size="large"
            fix></Spin>
    </div>
    <div slot="footer"
         class="button-panel-button">
      <i-button @click="close">返回</i-button>
    </div>
  </Modal>
</template>
<script>
import $ from '@/common/index';
import ModelComponent from '@/node/gridPanel/index';
import { billParser } from '@/common/parser';
export default {
	props: {
		value: {},
	},
	data() {
		return {
			show: false,
			modelPath: 'service/{app}/bill/model/tenantry',
			switchPath: 'service/{app}/switch',
			model: null,
		};
	},
	watch: {
		show(v) {
			this.$emit('input', v);
		},
		value(v) {
			if (v && !this.model) {
				this.getModel();
			}
			this.show = v;
		},
	},
	methods: {
		getModel() {
			$.link(this.modelPath).then(res => {
				this.model = billParser(res.data);
				this.model.actions = this.model.actions || {};
				this.model.actions.push(
					{
						code: 'def',
						type: 'process',
						name: '设为默认',
						locate: 'inline',
						scope: 'data',
						path: 'service/{app}/account/tenantry/def',
					},
					{
						code: 'addon',
						icon: 'checkmark',
						name: '切换',
						locate: 'inline',
						_button_type: 'success',
					}
				);
			});
		},
		listActionTrigger(data) {
			if (data.code === 'addon') {
				$.link(this.switchPath + '?tenantry_id=' + data.row.id, {
					//tenantry_switch: data.row.id,
				}).then(({ data }) => {
					var user = $.local('user');
					user[window.App].current_tenantry = data.current_tenantry;
					$.local('user', user);
					window.location.reload();
				});
			}
		},
		close() {
			this.show = false;
		},
	},
	components: {
		ModelComponent,
	},
};
</script>

<style lang="less">
.modal-collector {
	.field-control {
		margin-bottom: 16px;
		&.field-control-error {
			margin-bottom: 22px;
		}
	}
}
</style>
